<?php

class Membership_Api_Membership
{

	const QUOTA_ITEM_LIST = 500;
	/**
	 * @param  Database_Model_Item  $resource
	 * @param  User_Table_Model_User  $user
	 * @param  int $listId
	 * @return TRUE
	 */
	function addToList($resource, $user, $listId)
	{
		$table = Nuo::getTable('membership/data');
		return $table -> insertIgnore(array(
			'list_id' => $listId,
			'member_id' => $user -> getItemId(),
			'user_id' => $user -> getId(),
		));
	}

	/**
	 * create member list
	 *
	 */
	function createList($resource, $data)
	{
		$table = Nuo::getTable('membership/list');
		$obj = $table -> fetchNew();
		$obj -> resource_id = $resource -> getItemId();
		$obj -> title = $data['title'];
		$obj -> save();

		return $obj;
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE
	 */
	function addMember($resource, $user)
	{

		$table = Nuo::getTable('membership/member');

		$table -> insertIgnore(array(
			'resource_id' => $resource -> getItemId(),
			'member_id' => $user -> getItemId(),
			'user_id' => $user -> getId(),
		));

		return TRUE;
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE
	 */
	function removeMember($resource, $user)
	{
		$table = Nuo::getTable('membership/member');

		$table -> delete(array(
			'resource_id=?' => $resource -> getItemId(),
			'member_id=?' => $user -> getItemId(),
		));
		return TRUE;
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE/FALSE
	 */
	function isMember($resource, $user)
	{
		$table = Nuo::getTable('membership/member');
		$db = $table -> getAdapter();
		$sql = 'SELECT * FROM :table WHERE resource_id=:id AND member_id=:member_id';
		$sql = strtr($sql, array(
			':table' => $table -> getName(),
			':id' => $db -> quote($resource -> getItemId(), 'string'),
			':member_id' => $db -> quote($user -> getItemId()),
		));
		return $db -> fetchOne($sql);
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE/FALSE
	 */
	function isMemberOfMember($resource, $user)
	{
		$table = Nuo::getTable('membership/member');
		$db = $table -> getAdapter();

		$sql = <<<SQL
SELECT f2.resource_id
FROM :table AS f1
JOIN :table AS f2 ON (f2.member_id = f2.resource_id) 
WHERE f1.resource_id=:resourceId AND f2.member_id=:memberId LIMIT 1
SQL;

		$sql = strtr($sql, array(
			':table' => $table -> getName(),
			':memberId' => $db -> quote($user -> getItemId(), 'string'),
			':resourceId' => $db -> quote($resource -> getItemId(), 'string')
		));

		return $db -> fetchOne($sql);
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE/FALSE
	 */
	function isMemberInList($resource, $user)
	{

	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return TRUE/FALSE
	 */
	function isMemberInNetworks($resource, $user)
	{

	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @return TRUE
	 */
	function getMemberLists($resource)
	{
		$table = Nuo::getTable('membership/list');
		$select = $table -> select();
		$select -> where('resource_id=?', $resource -> getItemId());

		/**
		 * MAXIMUM LIST
		 */
		$select -> limit(self::QUOTA_ITEM_LIST);

		return $table -> fetchAll($select);
	}

	/**
	 * @param  Database_Model_Item    $resource
	 * @param  User_Table_Model_User  $user
	 * @return array
	 */
	function getListIdOfMember($resource, $user)
	{

		$listTable = Nuo::getTable('membership/list');
		$listDataTable = Nuo::getTable('membership/data');
		$db = $listTable -> getAdapter();

		// check all list of current viewer that join from user id
		$sql = <<<SQL
SELECT 
ul1.list_id, ml1.user_id
FROM :listTable AS ul1
JOIN :listDataTable AS ml1 ON (ul1.list_id = ml1.list_id AND ml1.member_id=:memberId)
WHERE ul1.resource_id=:resourceId
SQL;
		$sql = strtr($sql, array(
			':listTable' => $listTable -> getName(),
			':listDataTable' => $listDataTable -> getName(),
			':resourceId' => $db -> quote($resource -> getItemId(), 'string'),
			':memberId' => $db -> quote($user -> getItemId(), 'string'),
		));

		return $db -> fetchPairs($sql);
	}

	/**
	 * @param  Database_Model_Item  $resource1
	 * @param  Database_Model_Item  $resource2
	 * @return int
	 */
	function getMutualMemberCount($resource1, $resource2)
	{
		if (!$resource1 or !$resource2)
		{
			return 0;
		}
		// item1 === item2
		if ($resource1 -> getItemId() == $resource2 -> getItemId())
		{
			return 0;
		}

		$table = Nuo::getTable('membership/member');

		$db = $table -> getAdapter();

		$sql = <<<THESQL
SELECT 
COUNT(1)
FROM :table AS t1 
WHERE
t1.resource_id=:id1
AND t1.member_id IN (
	SELECT member_id 
	FROM :table 
	WHERE resource_id=:id2
)
THESQL;

		$sql = strtr($sql, array(
			':table' => $table -> getName(),
			':id1' => $db -> quote($resource1 -> getItemId(), 'string'),
			':id2' => $db -> quote($resource2 -> getItemId(), 'string'),
		));

		return $db -> fetchOne($sql);
	}

	/**
	 * @param Database_Model_Item $resource1
	 * @return int
	 */
	function getMemberCount($resource)
	{
		$table = Nuo::getTable('membership/member');

		$db = $table -> getAdapter();

		$sql = <<<SQL
SELECT COUNT(1) FROM :table WHERE resource_id=:resource_id
SQL;
		$sql = strtr($sql, array(
			':table' => $table -> getName(),
			':resource_id' => $db -> quote($resource -> getItemId(), 'string')
		));

		return $db -> fetchOne($sql);
	}

}
